<template>
  <Panel>
    <template #title>管理员信息</template>
    <template>
      <!--  {{ users }} -->
      <ul>
        <li>
          <span>管理员id:</span>
          <p>{{ users.id }}</p>
        </li>
        <li>
          <span>账号:</span>
          <p>{{ users.account }}</p>
        </li>
        <li>
          <span>用户组:</span>
          <p>{{ users.userGroup }}</p>
        </li>
        <li>
          <span>创建时间:</span>
          <p>{{ users.ctime }}</p>
        </li>
        <li>
          <span>管理员头像:</span>
          <!--  头像 -->
          <!-- <el-avater size="medium" :src="UserInfo.imgUrl"></el-avater> -->
          <!-- <p>
            <img :src="UserInfo.imgUrl" class="imgall" />
          </p> -->
          <!-- <img :src="users.imgUrl" alt="" class="imgall" /> -->
          <el-upload
            class="avatar-uploader"
            action="http://localhost:5000/users/avatar_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </li>
        <li>
          <el-button size="mini" type="primary" @click="seveAvatar"
            >确认头像</el-button
          >
        </li>
      </ul>
    </template>
  </Panel>
</template>

<script>
import local from '../../utils/local.js'
import { avater } from '../../api/user.js'
export default {
  data() {
    return {
      users: {},
      imageUrl: '',
      imgStr: ''
    }
  },
  created() {
    this.users = JSON.parse(local.get('users'))
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      // 把上传头像成功之后的图片地址保存起来
      this.imgStr = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const arr = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
      /* 判断当前的值是否在该数组中 */
      const isBool = arr.includes(file.type)
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isBool) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isBool && isLt2M
    },
    async seveAvatar() {
      const { code, msg } = await avater({
        imgUrl: this.imgStr
      })
      // console.log(data)
      if (code === 0) {
        this.$message({
          type: 'success',
          message: msg
        })
        location.reload()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.imgall {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  vertical-align: middle;
  margin-left: 10px;
}
li {
  display: flex;
  line-height: 15px;
  margin: 10px 0;
  border-bottom: 1px solid gray;
  padding: 10px;
  span {
    flex: 0 0 100px;
    margin-right: 10px;
  }
}
// /deep/穿刺写法
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
